package com.example.vpnservicedemo;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.VpnService;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private ActivityResultLauncher<Intent> launcher;

    private Intent intent;

    private BroadcastReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //vpn状态
        TextView textView = findViewById(R.id.vpnStatus);

        //回调判断启动vpn
        launcher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        intent = new Intent(this, MyVpnService.class);
                        intent.putExtra("status", "start");
                        startService(intent);
                        textView.setText("运行");
                    } else {

                    }
                }
        );

        //开关事件
        Switch switchButton = findViewById(R.id.vpnOn);
        switchButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {

                Intent vpnIntent = VpnService.prepare(this);
                if (vpnIntent != null) {
                    launcher.launch(vpnIntent);
                } else {
                    intent = new Intent(this, MyVpnService.class);
                    intent.putExtra("status", "start");
                    startService(intent);
                    textView.setText("运行");
                }

            } else {

                intent.putExtra("status", "stop");
                startService(intent);
                textView.setText("关闭");
            }
        });

        //已接受
        TextView readView = findViewById(R.id.readText);
        //已发送
        TextView writeView = findViewById(R.id.writeText);

        // 创建并注册广播接收器
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String read = intent.getStringExtra("read");
                String write = intent.getStringExtra("write");
                if (read != null) {
                    readView.setText(read);
                }
                if (write != null) {
                    writeView.setText(write);
                }


            }
        };

        registerReceiver(receiver, new IntentFilter("com.example.UPDATE_TEXT"));
    }

    public void sendHttp(View view) throws IOException {
        new Thread(() -> {
            try {
                StringBuilder response = new StringBuilder();

                URL url = new URL("https://www.baidu.com");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                try {
                    InputStream in = urlConnection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));

                    String line;
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                } finally {
                    urlConnection.disconnect();
                }
                System.out.println(response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();

    }


}